package models.services;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import models.entities.Colonia;
import models.services.exc.EntityNotFoundException;
import models.services.utils.DBManager;

public class ServicioColonias implements ICatalog<Colonia> {

	private final static Logger logger = Logger
			.getLogger(ServicioColonias.class);

	public List<Colonia> obtenerListaDeColonias() {
		List<Colonia> colonias = new ArrayList<Colonia>();
		String sql = "select * from colonia";
		DBManager manager = DBManager.getInstance();
		ResultSet rsColonias = manager.consultar(sql);
		try {
			while (rsColonias.next()) {
				Colonia col = new Colonia();
				int id = rsColonias.getInt("id");
				String nombre = rsColonias.getString("nombre");
				col.setId(id);
				col.setNombre(nombre);
				colonias.add(col);
			}
		} catch (Exception e) {
			logger.error(e.getCause(), e);
		}
		return colonias;
	}

	@Override
	public boolean borrar(Colonia entity) {
		DBManager manager = DBManager.getInstance();
		return manager.borrar(entity.getId(), "colonia");
	}

	@Override
	public int guardarActualizar(Colonia colonia) {
		DBManager manager = DBManager.getInstance();
		String sql = "insert into colonia values(null,'" + colonia.getNombre()
				+ "')";
		int ret = manager.guardarActualizar(sql);
		return ret;
	}

	@Override
	public Colonia obtener(int id) throws EntityNotFoundException {
		Colonia colonia = new Colonia();
		String sql = "select * from colonia where id =" + id;
		DBManager manager = DBManager.getInstance();
		ResultSet rsColonia = manager.consultar(sql);
		try {
			if (rsColonia.next()) {
				do {
					int idGet = rsColonia.getInt("id");
					String nombre = rsColonia.getString("nombre");
					colonia.setId(idGet);
					colonia.setNombre(nombre);
				} while (rsColonia.next());
			} else {
				throw new EntityNotFoundException("Colonia no encontrada");
			}
		} catch (SQLException e) {
			logger.error(e.getCause(), e);
		}
		return colonia;
	}

	public int update(String campo, String value, int id) {
		DBManager manager = DBManager.getInstance();
		String sql = "update colonia set " + campo + " = '" + value
				+ "' where id=" + id;
		int guardarActualizar = manager.guardarActualizar(sql);
		return guardarActualizar;
	}

	public int update(String campo, int value, int id) {
		DBManager manager = DBManager.getInstance();
		String sql = "update colonia set " + campo + " = " + value
				+ " where id=" + id;
		int guardarActualizar = manager.guardarActualizar(sql);
		return guardarActualizar;

	}

	@Override
	public List<Colonia> buscarPorNumero(String campo, int valor) {
		List<Colonia> colonias = new ArrayList<Colonia>();
		DBManager manager = DBManager.getInstance();
		String sql = "select * from colonia where " + campo + " = " + valor;
		ResultSet rs = manager.consultar(sql);
		return this.buscar(rs, colonias);
	}

	@Override
	public List<Colonia> buscarPorString(String campo, String valor) {
		List<Colonia> colonias = new ArrayList<Colonia>();
		DBManager manager = DBManager.getInstance();
		String sql = "select * from colonia where " + campo + " like '%"
				+ valor + "%'";
		ResultSet rs = manager.consultar(sql);
		return this.buscar(rs, colonias);
	}

	private List<Colonia> buscar(ResultSet rsColonias, List<Colonia> colonias) {
		try {
			while (rsColonias.next()) {
				Colonia col = new Colonia();
				int id = rsColonias.getInt("id");
				String nombre = rsColonias.getString("nombre");
				col.setId(id);
				col.setNombre(nombre);
				colonias.add(col);
			}
		} catch (Exception e) {
			logger.error(e.getCause(), e);
			return null;
		}
		return colonias;
	}

}
